unit MainDataModuleUnit;

interface

uses
  System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, Data.DB, FireDAC.Comp.Client, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, FireDAC.Phys.IBBase,
  FireDAC.Phys.FB, Data.DBXJSON;

type
  TScuolaMaternaDataModule = class(TDataModule)
    Connection: TFDConnection;
    FDPhysFBDriverLink1: TFDPhysFBDriverLink;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    function GetElencoAlunni(search : String) : TJSONArray;
  end;


implementation

uses ObjectsMappers;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

{ TScuolaMaternaDataModule }

procedure TScuolaMaternaDataModule.DataModuleCreate(Sender: TObject);
begin
  Connection.Params.Values['Database'] := 'SCUOLAMATERNA.FDB';
  Connection.Open;
end;

function TScuolaMaternaDataModule.GetElencoAlunni(search: String): TJSONArray;
var
  obj: TJSONObject;
  query: TFDQuery;
begin
  query := TFDQuery.Create(Connection);
  try
    if Search.IsEmpty then
      query.SQL.Add('SELECT * FROM Alunni')
    else
      query.SQL.Add('SELECT * FROM Alunni where Nome CONTAINING ''' + Search + '''');
    query.SQL.Add(' order by Cognome, Nome');
    query.Open;
    if not query.IsEmpty then begin
      Result := TJSONArray.Create;
      Mapper.DataSetToJSONArray(query, Result);
    end;
  finally
    query.free;
  end;
end;

end.
